package com.zut.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.zut.pojo.Employee;
import com.zut.pojo.PageInfo;
import com.zut.service.EmployeeService;
import com.zut.service.impl.EmployeeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 张晨
 * @date 2024/9/20 18:16
 * @desc
 */
@WebServlet("/employee")
public class EmployeeController extends HttpServlet {
    private EmployeeService employeeService = new EmployeeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = req.getParameter("op");
        if (StringUtils.isEmpty(op)){
            resp.getWriter().write("参数错误");
            return;
        }
        switch (op){
            case "sel":
                sel(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "del":
                del(req,resp);
                break;
            case "upd":
                upd(req,resp);
                break;
        }
    }

    private void sel(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        if(StringUtils.isEmpty(currentPage)){
            currentPage = "1";
        }
        if(StringUtils.isEmpty(pageSize)){
            pageSize = "5";
        }

        String account = req.getParameter("account");
        String nickname = req.getParameter("nickname");
        String telephone = req.getParameter("telephone");

        Employee employee = new Employee();

        employee.setAccount(account);
        employee.setNickname(nickname);
        employee.setTelephone(telephone);

        PageInfo pageInfo = employeeService.conditionSelect(employee, Integer.parseInt(currentPage),Integer.parseInt(pageSize));

        String jsonString = JSON.toJSONString(pageInfo);

        resp.getWriter().write(jsonString);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String nickname = req.getParameter("nickname");
        String tel = req.getParameter("telephone");
        String role = req.getParameter("role");
        // String isDel = req.getParameter("is_del");

        Employee employee = new Employee();
        employee.setAccount(account);
        employee.setPassword(password);
        employee.setNickname(nickname);
        employee.setTelephone(tel);
        employee.setRole(role);
        // employee.setIs_del(Integer.parseInt(isDel));

        Integer count = employeeService.addEmployee(employee);


        resp.getWriter().write(count+"");
    }

    private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Integer count = employeeService.deleteEmployeeById(Integer.parseInt(id));

        resp.getWriter().write(count+"");
    }

    private void upd(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        Integer id = Integer.parseInt(req.getParameter("id"));
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String nickname = req.getParameter("nickname");
        String tel = req.getParameter("telephone");
        String role = req.getParameter("role");
        String isDel = req.getParameter("isDel");


        Employee employee = new Employee();
        employee.setId(id);
        employee.setAccount(account);
        employee.setPassword(password);
        employee.setNickname(nickname);
        employee.setTelephone(tel);
        employee.setRole(role);
        employee.setIsDel(Integer.parseInt(isDel));

        Integer count = employeeService.updateById(employee);
        resp.getWriter().write(count+"");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
